package com.qbw.core.fragment;

import android.app.Dialog;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.qbw.app.R;
import com.qbw.app.databinding.CoreFragmentLoadingBinding;
import com.qbw.core.base.BaseDialogFragment;


/*
 * Created by QBW on 2019/6/18
 */
public class LoadingDialogFragment extends BaseDialogFragment<CoreFragmentLoadingBinding> {

    private static final String KEY_TITLE = "key_title";
    private String mTitle;

    public static LoadingDialogFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString(KEY_TITLE, title);
        LoadingDialogFragment fragment = new LoadingDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int onGetLayoutId() {
        return R.layout.core_fragment_loading;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mTitle = args.getString(KEY_TITLE);
        mBinding.tv.setVisibility(TextUtils.isEmpty(mTitle) ? View.GONE : View.VISIBLE);
        mBinding.tv.setText(mTitle);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_TITLE, mTitle);
    }

    public void setTitle(String title) {
        mTitle = title;
        if (mBinding != null) {
            mBinding.tv.setText(title);
        }
    }
}
